<?php

namespace app\api\controller\Product;

use think\Controller;
use think\Request;

class Product extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->TypeModel = model('Product.Type');

        $this->ProductModel = model('Product.Product');
    }

    public function index()
    {
        if($this->request->isPost())
        {
            $TypeList = $this->TypeModel->order('weigh','asc')->select();

            $NewList  = $this->ProductModel->where('flag',1)->order('id','desc')->limit(4)->select();

            $Recommend = $this->ProductModel->where('flag',3)->order('id','desc')->limit(6)->select();

            $HotList = $this->ProductModel->where('flag',2)->order('id','desc')->limit(6)->select();


            $data = [
                'typelist' => $TypeList,
                'newlist' => $NewList,
                'recommend' => $Recommend,
                'hotlist' => $HotList
            ];

            $this->success('返回首页数据',null,$data);
        }
    }

    // 商品列表
    public function list()
    {
        if($this->request->isPost())
        {
            $typeid = $this->request->param('typeid',0);

            $flag = $this->request->param('flag',0);

            $OrderBy = $this->request->param('OrderBy','id');

            $keyword = $this->request->param('keyword','');

            // halt($typeid);

            $type = $this->TypeModel->find($typeid);

            $where = [];

            if($type)
            {
                $where['typeid'] = $typeid;
            }

            if($flag)
            {
                $where['flag'] = $flag;
            }

            if($keyword)
            {
                $where['name'] = ['like',"%$keyword%"];
            }

            $TypeList = $this->TypeModel->select();

            $list = $this->ProductModel->where($where)->order($OrderBy,'desc')->select();

            $data = [
                'TypeList' => $TypeList,
                'ProList' => $list
            ];

            $this->success('返回商品列表数据成功',null,$data);
        }
    }

    public function info()
    {
        if($this->request->isPost())
        {
            $proid = $this->request->param('proid',0);

            $product = $this->ProductModel->with(['type'])->find($proid);

            if(!$product)
            {
                $this->error('选择的商品不存在');
                exit;
            }

            $this->success('返回商品详情数据',null,$product);
        }
    }
    /**
     * 
     * uinapp小程序 
     * */
    public function recommend()
    {
        if($this->request->isPost())
        {
            $Recommend = $this->ProductModel->where('flag',3)->order('id','desc')->limit(6)->select();

            $this->success('返回商品推荐数据',null,$Recommend);
        }
    }
}
